package com.example.liyun.joy.serviceutils;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.functions.Function;

/**
 * 创建一个统一处理数据Loader类，处理后返回需要的数据
 * 统一处理数据，继承ObjectLoader
 *
 * 因为请求接口不同，可以在这里设置请求数据的判断
 */
public class LoaderResponce extends ObjectLoader{
    //创建网络请求接口实例
    private ServiceApi mApiRequest;
    public LoaderResponce(){
        //初始化实例
        mApiRequest=RetrofitServiceManager.getInstance().creat(ServiceApi.class);
    }



    /**
     * 请求笑话并返回数据的方法
     * @param page
     * @param pagesize
     * @return
     */
    public Observable<List<JokeBean.ResultBean.DataBean>> getDatas(int page,int pagesize){
        return observa(mApiRequest.getJokeDatas(ApiConfig.JOKEKEY,page,pagesize))
                    .map(new Function<JokeBean, List<JokeBean.ResultBean.DataBean>>() {
                        @Override
                        public List<JokeBean.ResultBean.DataBean> apply(JokeBean jokeBean) throws Exception {
                            return jokeBean.getResult().getData();
                        }
                    });
    }
    /*public <T> Observable<List<T>> getData(){
        return observa(mApiRequest.).map(new Function<Object, List<T>>() {
            @Override
            public List<T> apply(Object o) throws Exception {
                return null;
            }
        });
    }*/

    /**
     * 请求新闻并返回数据的方法
     * @param type
     * @return
     */
    public Observable<List<NewsBean.ResultBean.DataBean>> getDatas(String type){
        return observa(mApiRequest.getNewsDatas(ApiConfig.NEWSKEY,type))
                    .map(new Function<NewsBean, List<NewsBean.ResultBean.DataBean>>() {
                        @Override
                        public List<NewsBean.ResultBean.DataBean> apply(NewsBean newsBean) throws Exception {
                            return newsBean.getResult().getData();
                        }
                    });
    }
}
